package com.gxa.controller;

import com.gxa.BusinessException;
import com.gxa.pojo.Permission;
import com.gxa.pojo.User;
import com.gxa.service.PermissionService;
import com.gxa.service.UserService;
import com.gxa.utils.JWTUtil;
import com.gxa.utils.JsonResult;
import com.gxa.utils.MD5Util;
import com.gxa.utils.PowerTreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Tian同學
 * @Date 2020/10/1 21:59
 * @Version 1.0
 */

@Controller
@RequestMapping("/user")
@Api(value = "执行登录操作的控制器", tags = "登录接口的API")
@CrossOrigin
@Slf4j
public class LoginController {

    @Resource
    UserService userService;
    @Autowired
    PermissionService permissionService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "#####登录接口#####", notes = "登录的接口", httpMethod = "post", response = String.class)
    public Map<String, Object> login(@RequestBody Map map) throws BusinessException {
        String userName = map.get("userName").toString();
        String passwd = map.get("passwd").toString();
        System.out.println("userName:" + userName);
        System.out.println("passwd:" + passwd);
        JsonResult jsonResult = new JsonResult();
        User login = null;
        // 做数据校验（数据格式的合法性，使用第三方工具）

        if ((userName != null && !userName.equals("")) && (passwd != null && !passwd.equals(""))) {
            // 调用对应的service执行登录验证方法(通过Spring给service对象注入)
            login = userService.login(userName, passwd);
            if (login == null) {
                jsonResult.setCode("402");
                jsonResult.setMsg("账号或密码错误~~~");
                return jsonResult.getValues();
            }
            Integer state = login.getState();
            if (state == 0) {
                jsonResult.setCode("406");
                jsonResult.setMsg("该账号已被禁用~~~");
                return jsonResult.getValues();
            }
            Integer i = permissionService.findPermissionByUserName1(userName);
            if (i==0){
                jsonResult.setCode("407");
                jsonResult.setMsg("该账号没有权限");
                return jsonResult.getValues();
            }
        } else {
            jsonResult.setCode("403");
            jsonResult.setMsg("账号和密码都不能为空~~~");
            return jsonResult.getValues();
        }


        // 把登录验证结果给前端反馈
        if (login != null) {
            //该账户所拥有的权限
            List<Permission> permissions = permissionService.findPermissionByUserName(userName);
            //权限树状图
            List<Permission> treeList = PowerTreeUtil.getTreeList(permissions);
            System.out.println(treeList);
            //所有权限
            List<Permission> Permissions = permissionService.findAllPermission();
            List<Permission> allPermission = PowerTreeUtil.getTreeList(Permissions);
            System.out.println(allPermission);
            jsonResult.setData(treeList);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功:");
            jsonResult.put("login", login);
            //登录成功 设置jwt
            JWTUtil util = new JWTUtil();
            //设置信息
            Map<String, Object> payload = new HashMap<String, Object>();
            payload.put("userID", login.getUserID());
            try {
                jsonResult.put("jwt", util.createJWT("jwt", "", 8000000, payload));
                jsonResult.put("allPermission", allPermission);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return jsonResult.getValues();
    }
}


